package org.example.realm;

import org.apache.ibatis.session.SqlSession;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.example.domain.Employee;
import org.example.mapper.EmployeeMapper;
import org.example.util.MyBatisUtil;

/**
 * @author 李新雨的项目86176
 * @version 1.0
 * @date 2024-12-15 11:16
 */
public class EmailRealm  extends AuthorizingRealm {

    @Override
    public String getName() {
        return "emailRealm";
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("EmailRealm");
        UsernamePasswordToken usernamePasswordToken= (UsernamePasswordToken) token;
//        Object principal = token.getPrincipal();//获取用户名
        String email = usernamePasswordToken.getUsername();
        System.out.println(email);
        SqlSession session = MyBatisUtil.getSession();
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        Employee employee=employeeMapper.selByEmailGetEmployee(email);
        if(employee==null) {//用户不存在
            return null;
        }
        System.out.println("emailRealm:"+employee);
        return new SimpleAuthenticationInfo(employee,employee.getPassword(),getName());

    }
}
